package com.srit.recipes.common.utils;

import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesUtil {

    private static final Properties prop = new Properties();

    private static final String DEFAULT_URL = "http://36.133.84.220:8075/zhccypt/screen/insertScene";

    private static final String UPLOAD_KEY = "upload_url";


    private static final String PIC_PATH = "picPath";
    private static final String DEFAULT_PIC_PATH = "/home/leslie/ftpd/";


    static {
        try {
            File config = new File("native/nativeConfig.properties");
            if (!config.exists()) {
                config.getParentFile().mkdir();
                config.createNewFile();
            }
            prop.load(new FileInputStream(config));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static String getRectifyHeatBeatUrl() {
        return prop.getProperty("rectifyHeatBeatUrl");
    }

    public static String getRectifyUploadUrl() {
        return prop.getProperty("rectifyUploadUrl");
    }


    public static String getUploadUrl() {
        String upload_url = prop.getProperty(UPLOAD_KEY);
        if (StringUtils.isEmpty(upload_url)) {
            changeUrl(DEFAULT_URL);
            return DEFAULT_URL;
        }
        return upload_url;
    }

    public static String getPicPath() {
        return "E:\\projects\\tbas\\image";
    }

    public static String getNativeUploadUrl() {
        return prop.getProperty("nativeUpload");
    }


    public static void changeUrl(String url) {
        prop.setProperty(UPLOAD_KEY, url);
        try {
            prop.store(new FileOutputStream("native/nativeConfig.properties"), "upload to center by this url");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
